﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Threading;
using HaoLab.WindowsPhone;
using Microsoft.Devices;
using Microsoft.Phone.Controls;
using YinYueTai.HtmlParser;
using YinYueTai.Model;

namespace YinYueTai.ViewModel
{
    /// <summary>
    /// This class contains properties that a View can data bind to.
    /// <para>
    /// Use the <strong>mvvminpc</strong> snippet to add bindable properties to this ViewModel.
    /// </para>
    /// <para>
    /// See http://www.galasoft.ch/mvvm/getstarted
    /// </para>
    /// </summary>
    public class FocusViewModel : ViewModelBase
    {
        /// <summary>
        /// Initializes a new instance of the FocusViewModel class.
        /// </summary>
        public FocusViewModel()
        {

            NetworkInfomation.CheckWifiEnable("系统检测到您没有WIFI连接！本程序提供高清MV播放，对网速和流量要求较高，希望您使用WIFI进行使用！");
            DispatcherHelper.Initialize();

            Focus f = new Focus("", mv => FirstCollection = new List<MV>(mv));
            f.GetInfo();

        }

        private RelayCommand<MV> command1;

        /// <summary>
        /// Gets the Command1.
        /// </summary>
        public RelayCommand<MV> Command1
        {
            get
            {
                return command1
                    ?? (command1 = new RelayCommand<MV>(
                                          p =>
                                          {
                                              if (p != null)
                                              {
                                                  (Application.Current as App).RootFrame.Navigate(new Uri("/View/VideoView.xaml?Url=" + p.ID, UriKind.Relative));
                                                  if (VideoViewModel.mv == null || VideoViewModel.mv.ID != p.ID)
                                                      VideoViewModel.mv = new MvDetail(p);
                                              }
                                          }));
            }
        }

        private RelayCommand<SelectionChangedEventArgs> _panoramaChanged;

        /// <summary>
        /// Gets the PanoramaChanged.
        /// </summary>
        public RelayCommand<SelectionChangedEventArgs> PanoramaChanged
        {
            get
            {
                return _panoramaChanged
                    ?? (_panoramaChanged = new RelayCommand<SelectionChangedEventArgs>(
                                          p =>
                                          {
                                              if (p.AddedItems.Count > 0)
                                                  BindingFactory(((p.AddedItems[0] as PanoramaItem).Header as TextBlock).Text);
                                              //RaisePropertyChanged("UsCollection");
                                          }));
            }
        }

        private RelayCommand<System.ComponentModel.CancelEventArgs> _backKeyCommand;

        /// <summary>
        /// Gets the BackKeyCommand.
        /// </summary>
        public RelayCommand<System.ComponentModel.CancelEventArgs> BackKeyCommand
        {
            get
            {
                return _backKeyCommand
                    ?? (_backKeyCommand = new RelayCommand<System.ComponentModel.CancelEventArgs>(
                                          p =>
                                          {
                                              VibrateController vc = VibrateController.Default;
                                              vc.Start(TimeSpan.FromMilliseconds(100));
                                              if (MessageBox.Show("您确定要退出本程序吗？", "提醒", MessageBoxButton.OKCancel) == MessageBoxResult.Cancel)
                                                  p.Cancel = true;
                                          }));
            }
        }

        private List<MV> usCollection = new List<MV>();
        public List<MV> UsCollection
        {
            get
            {
                return usCollection;
            }
            set
            {

                usCollection = value;
                RaisePropertyChanged("UsCollection");
            }
        }

        /// <summary>
        /// The <see cref="HtCollection" /> property's name.
        /// </summary>
        public const string HtCollectionPropertyName = "HtCollection";

        private List<MV> _htCollection = new List<MV>();

        /// <summary>
        /// Sets and gets the HtCollection property.
        /// Changes to that property's value raise the PropertyChanged event. 
        /// </summary>
        public List<MV> HtCollection
        {
            get
            {
                return _htCollection;
            }

            set
            {
                if (_htCollection == value)
                {
                    return;
                }

                _htCollection = value;
                RaisePropertyChanged(HtCollectionPropertyName);
            }
        }

        /// <summary>
        /// The <see cref="FirstCollection" /> property's name.
        /// </summary>
        public const string FirstCollectionPropertyName = "FirstCollection";

        private List<MV> _firstCollection = new List<MV>();

        /// <summary>
        /// Sets and gets the FirstCollection property.
        /// Changes to that property's value raise the PropertyChanged event. 
        /// </summary>
        public List<MV> FirstCollection
        {
            get
            {
                return _firstCollection;
            }

            set
            {
                if (_firstCollection == value)
                {
                    return;
                }

                _firstCollection = value;
                RaisePropertyChanged(FirstCollectionPropertyName);
            }
        }

        /// <summary>
        /// The <see cref="MlCollection" /> property's name.
        /// </summary>
        public const string MlCollectionPropertyName = "MlCollection";

        private List<MV> _mlCollction = new List<MV>();

        /// <summary>
        /// Sets and gets the MlCollection property.
        /// Changes to that property's value raise the PropertyChanged event. 
        /// </summary>
        public List<MV> MlCollection
        {
            get
            {
                return _mlCollction;
            }

            set
            {
                if (_mlCollction == value)
                {
                    return;
                }

                _mlCollction = value;
                RaisePropertyChanged(MlCollectionPropertyName);
            }
        }

        /// <summary>
        /// The <see cref="KrCollection" /> property's name.
        /// </summary>
        public const string KrCollectionPropertyName = "KrCollection";

        private List<MV> _krCollection = new List<MV>();

        /// <summary>
        /// Sets and gets the KrCollection property.
        /// Changes to that property's value raise the PropertyChanged event. 
        /// </summary>
        public List<MV> KrCollection
        {
            get
            {
                return _krCollection;
            }

            set
            {
                if (_krCollection == value)
                {
                    return;
                }

                _krCollection = value;
                RaisePropertyChanged(KrCollectionPropertyName);
            }
        }

        /// <summary>
        /// The <see cref="JpCollection" /> property's name.
        /// </summary>
        public const string JpCollectionPropertyName = "JpCollection";

        private List<MV> _jpCollection = new List<MV>();

        /// <summary>
        /// Sets and gets the JpCollection property.
        /// Changes to that property's value raise the PropertyChanged event. 
        /// </summary>
        public List<MV> JpCollection
        {
            get
            {
                return _jpCollection;
            }

            set
            {
                if (_jpCollection == value)
                {
                    return;
                }

                _jpCollection = value;
                RaisePropertyChanged(JpCollectionPropertyName);
            }
        }

        /// <summary>
        /// The <see cref="EliteCollection" /> property's name.
        /// </summary>
        public const string EliteCollectionPropertyName = "EliteCollection";

        private List<MV> _eliteCollection = new List<MV>();

        /// <summary>
        /// Sets and gets the EliteCollection property.
        /// Changes to that property's value raise the PropertyChanged event. 
        /// </summary>
        public List<MV> EliteCollection
        {
            get
            {
                return _eliteCollection;
            }

            set
            {
                if (_eliteCollection == value)
                {
                    return;
                }

                _eliteCollection = value;
                RaisePropertyChanged(EliteCollectionPropertyName);
            }
        }


        private void BindingFactory(string index)
        {
            switch (index)
            {
                case "首播":
                    if (FirstCollection.Count == 0)
                        new Focus("", mv => FirstCollection = new List<MV>(mv)).GetInfo();
                    break;
                case "港台":
                    if (HtCollection.Count == 0)
                        new Focus("index-ht", mv => HtCollection = new List<MV>(mv)).GetInfo();
                    break;
                case "内地":
                    if (MlCollection.Count == 0)
                        new Focus("index-ml", mv => MlCollection = new List<MV>(mv)).GetInfo();
                    break;
                case "欧美":
                    if (UsCollection.Count == 0)
                        new Focus("index-us", mv => UsCollection = new List<MV>(mv)).GetInfo();
                    break;
                case "韩国":
                    if (KrCollection.Count == 0)
                        new Focus("index-kr", mv => KrCollection = new List<MV>(mv)).GetInfo();
                    break;
                case "日本":
                    if (JpCollection.Count == 0)
                        new Focus("index-jp", mv => JpCollection = new List<MV>(mv)).GetInfo();
                    break;
                case "热门推荐":
                    if (EliteCollection.Count == 0)
                        new Focus("index-elite", mv => EliteCollection = new List<MV>(mv)).GetInfo();
                    break;
                default:
                    break;
            }
        }


    }
}